<template>
  <div class="myPaging">
    <el-pagination
      style="float: right;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from "vuex";
export default {
  name: "myPaging",
  props: {
    func: {
      type: Function,
      required: true
    },
    total: {
      type: Number,
      required: true
    },
    pageSizes: {
      type: Array,
      default: () => {
        return [12, 50, 100];
      }
    }
  },
  data() {
    return {
      msg: "hello myPaging!"
    };
  },
  methods: {
    ...mapMutations([
      "initPageOption",
      "changePageSize",
      "changePageNum",

      "changeLastPage",
      "changeTotal"
    ]),
    handleSizeChange(val) {
      this.changePageSize(val);
      this.initPageOption({ total: this.total, pageSize: this.pageSize });
      this.func({ pageSize: this.pageSize, pageNum: 1 });
    },
    handleCurrentChange(val) {
      this.changePageNum(val);
      this.func({ pageSize: this.pageSize, pageNum: val });
    },
    clickPaging(type) {
      if (type === "first") {
        this.changePageNum(1);
      }
      if (type === "pre") {
        let num = this.pageNum;
        if (num <= 1) {
          this.$notify({
            title: "提示",
            type: "info",
            message: "当前已是第一页"
          });
          return;
        } else {
          this.changePageNum(num - 1);
        }
      }
      if (type === "next") {
        let num = this.pageNum;
        if (num >= this.lastPage) {
          this.$notify({
            title: "提示",
            type: "info",
            message: "当前已是最后一页"
          });
          return;
        } else {
          this.changePageNum(num + 1);
        }
      }
      if (type === "last") {
        this.changePageNum(this.lastPage);
      }
      this.func({ pageSize: this.pageSize, pageNum: this.pageNum });
    }
  },
  created() {
    this.initPageOption({ total: this.total, pageSize: this.pageSizes[0] });
  },
  computed: {
    ...mapGetters({
      pageSize: "getPageSize",
      pageNum: "getPageNum",
      lastPage: "getLastPage",
      dataTotal: "getDataTotal"
    })
  },
  watch: {
    total(val) {
      this.initPageOption({ total: this.total, pageSize: this.pageSize });
    }
  },
  beforeDestroy() {
    // 解决页面切换 页数保留的问题
    this.changePageNum(1);
  }
};
</script>
<style scoped>
.myPaging {
  overflow: hidden;
  padding: 20px 10px;
}
</style>

